import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Main extends JFrame implements ActionListener{
    JLabel jlabel1, jlabel2, jlabel3;
    JTextField jtf1, jtf2, jtf3;
    JButton jb1,jb2,jb3,jb4;

    Connection conn=null;
    Statement stmt=null;
    PreparedStatement pstmt=null;
    ResultSet resultSet=null;

    public Main(String title)
    {
        super(title);
        jlabel1 = new JLabel("书号：");
        jlabel2 = new JLabel("书名：");
        jlabel3 = new JLabel("作者：");
        jtf1 = new JTextField();
        jtf1.setEditable(false);
        jtf2 = new JTextField();
        jtf3 = new JTextField();
        jb1 = new JButton("查询");
        jb2 = new JButton("下一条");
        jb3 = new JButton("增加");
        jb4 = new JButton("修改");

        jb1.addActionListener(this);
        jb2.addActionListener(this);
        jb3.addActionListener(this);
        jb4.addActionListener(this);

        Container cp=this.getContentPane();
        cp.setLayout(new GridLayout(5,2,10,10));
        cp.add(jlabel1);
        cp.add(jtf1);
        cp.add(jlabel2);
        cp.add(jtf2);
        cp.add(jlabel3);
        cp.add(jtf3);
        cp.add(jb1);
        cp.add(jb2);
        cp.add(jb3);
        cp.add(jb4);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 200);
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main("图书信息管理系统");

    }

    public void getData()
    {
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "123456");
            stmt=conn.createStatement();
            String sql= " select * from book ";
            resultSet=stmt.executeQuery(sql);
            if(resultSet.next())
            {
                jtf1.setText(resultSet.getString(1));
                jtf2.setText(resultSet.getString(2));
                jtf3.setText(resultSet.getString(3));
            }
        }
        catch(Exception ex){System.out.println(ex.toString());}
    }


    public void nextData()
    {
        try{
            if(resultSet.next())
            {
                jtf1.setText(resultSet.getString(1));
                jtf2.setText(resultSet.getString(2));
                jtf3.setText(resultSet.getString(3));
            }
        }catch(Exception ex){System.out.println(ex.toString());}
    }



    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if (e.getSource() == jb1){
            getData();

        }
        if (e.getSource() == jb2)
        {
            nextData();
        }

    }
}


